﻿using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using eShop.Business;

namespace eShop.Utility
{
    public class WebPresentationUtility
    {
        public static string CopyImageAndResize(string sourceFilePath, string destinationFolderPath, string fileNameSuffix, int resizeWidth)
        {
            string destinationFileName = Guid.NewGuid().ToString();
            return CopyImageAndResize(sourceFilePath, destinationFolderPath, destinationFileName, fileNameSuffix, resizeWidth);
        }

        public static string CopyImageAndResize(string sourceFilePath, string destinationFolderPath, string destinationFileName, string fileNameSuffix, int resizeWidth)
        {
            FileInfo sourceImageFileInfo = new FileInfo(HttpContext.Current.Server.MapPath(sourceFilePath));
            if (!sourceImageFileInfo.Exists)
                throw new FileNotFoundException("Source image file not found.");
            string destinationFilePath = destinationFolderPath + destinationFileName + fileNameSuffix + sourceImageFileInfo.Extension;
            if (resizeWidth > 0)
            {
                destinationFilePath = ImageResizer.CreateThumbnail(sourceFilePath, destinationFilePath, resizeWidth, false, string.Empty);
            }
            else
            {
                FileAccessUtility.CopyFile(sourceFilePath, destinationFilePath);
            }
            return destinationFilePath;
        }

        public static string UploadImageAndValidate(string destinationFolderPath, HttpPostedFile uploadedFile)
        {
            if (uploadedFile.ContentLength > 0)
            {
                FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
                string imageFilePath = destinationFolderPath + Guid.NewGuid().ToString() + fileInfo.Extension;
                if (File.Exists(HttpContext.Current.Server.MapPath(imageFilePath)))
                    FileAccessUtility.DeleteFile(imageFilePath);
                string contentType = uploadedFile.ContentType.ToLower();
                if (contentType == "image/jpg" || contentType == "image/gif" || contentType == "image/png"
                    || contentType == "image/jpeg" || contentType == "image/bmp" || contentType == "image/pjpeg"
                    || contentType == "image/x-png")
                {
                    uploadedFile.SaveAs(HttpContext.Current.Server.MapPath(imageFilePath));
                    return imageFilePath;
                }
                else
                {
                    throw new Exception(ApplicationMessage.ImageFormateNotSupported);
                }
            }
            return string.Empty;
        }

        public static string FormatLineBreakForWeb(string input)
        {
            input.Replace("\n\r", "<br />");
            input.Replace("\n", "<br />");
            input.Replace("\r", "<br />");
            return input;
        }

        public static string CleanCurrency(string input)
        {
            return input.ToLower().Replace("$", "<br />").Replace("tk","");
        }
    }
}